﻿using System.IO;
using System.IO.Compression;
using System.Web.Mvc;
using System.Web.Routing;
using Library.Models;

namespace Library.MVCHelpers
{
    public class BaseController : Controller
    {
        private EcommerceEntities _databaseConnector;

        public string CurrentCulture
        {
            get { return NOWCulture.CurrentCulture; }
        }

        public EcommerceEntities DatabaseConnector
        {
            get { return _databaseConnector; }
        }

        protected override void Initialize(RequestContext requestContext)
        {
            if (_databaseConnector == null)
            {
                _databaseConnector = new EcommerceEntities();
            }
            base.Initialize(requestContext);
        }

        protected override void Dispose(bool disposing)
        {            
            if (_databaseConnector != null)
            {
                _databaseConnector.Dispose();
            }
            base.Dispose(disposing);
        }

        public string RenderPartialViewToString(string viewName, object model = null)
        {
            // first find the ViewEngine for this view
            var viewEngineResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);

            if (viewEngineResult == null)
                throw new FileNotFoundException("View cannot be found.");

            // get the view and attach the model to view data
            var view = viewEngineResult.View;
            ControllerContext.Controller.ViewData.Model = model;

            string result;

            using (var sw = new StringWriter())
            {
                var ctx = new ViewContext(ControllerContext, view, ControllerContext.Controller.ViewData, ControllerContext.Controller.TempData, sw);
                view.Render(ctx, sw);
                result = sw.ToString();
            }

            return result;
        }

        #region Gzip
        public class Compress : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                var request = filterContext.HttpContext.Request;

                var acceptEncoding = request.Headers["Accept-Encoding"];

                if (string.IsNullOrEmpty(acceptEncoding)) return;

                acceptEncoding = acceptEncoding.ToUpperInvariant();

                var response = filterContext.HttpContext.Response;

                if (response.Filter != null)
                {
                    if (acceptEncoding.Contains("GZIP"))
                    {
                        response.AppendHeader("Content-Encoding", "gzip");
                        response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
                    }
                    else if (acceptEncoding.Contains("DEFLATE"))
                    {
                        response.AppendHeader("Content-Encoding", "deflate");
                        response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
                    }
                }
            }
        }
        #endregion
    }
}